var userenterprise = false;

function reservationEnterprisesFormValidation() {

	if (conductorForm.form() && carForm.form() && reservationForm.form()) {

		objectToSend = new Array();
		dataToSend = false;
		reservation = new Array();

		var dataConfirm = new reservationData();

		if (conductorReserva)
		{
			$(".conductor-data-form").each(function() {

				if (conductorReserva[this.name] !== this.value)
					objectToSend.push(Array(this.name, this.value));
			});
			if (objectToSend.length) {
				conductor = conductorReserva['conductorid'];
				objectToSend.push(Array('conductorid', conductorReserva['conductorid']));
				dataToSend = {type: 'updateConductor', conductor: objectToSend};
			}

		}
		else {
			$(".conductor-data-form").each(function() {

				objectToSend.push(Array(this.name, this.value));
			});
			dataToSend = {type: 'insert', conductor: objectToSend, enterprise: true};
		}
		if (dataToSend) {
			$.ajax({
				beforeSend: function() {
					$('#okR').replaceWith('<img id="loading" style="width: 25px; height: 25px; margin-left: 10px" src="images/loading.gif"></img>');
				},
				complete: function() {
					$('#loading').replaceWith('<div id="okR" class="button_okR " style=" cursor: pointer; margin-top: 3px;" onclick="reservationEnterprisesFormValidation()"><label style="margin-left: 10px;cursor: inherit;">Login</label></div>');
				},
				dataType: "json",
				async: false,
				type: "POST",
				url: "../phpFiles/index.php",
				data: dataToSend
			}).done(function(msg) {

				if (msg)
				{
					conductor = msg['conductorid'];
					$.each(msg, function(key, val) {

						dataConfirm.reservation[ key ] = val;
					});
					dataConfirm.reservation[ 'conductor' ] = msg[ 'sex' ] + '. ' + msg[ 'name' ] + ' ' + msg[ 'last_name' ];
				}

			});
		}
		else {
			conductor = conductorReserva['conductorid'];
			dataConfirm.reservation[ "sex" ] = conductorReserva[ "sex" ];
			dataConfirm.reservation[ "name" ] = conductorReserva[ "name" ];
			dataConfirm.reservation[ "last_name" ] = conductorReserva[ "last_name" ];
			dataConfirm.reservation[ "postal_code" ] = conductorReserva[ "postal_code" ];
			dataConfirm.reservation[ "address" ] = conductorReserva[ "address" ];
			dataConfirm.reservation[ "phone_movil" ] = conductorReserva[ "phone_movil" ];
			dataConfirm.reservation[ 'conductor' ] = conductorReserva[ 'sex' ] + '. ' + conductorReserva[ 'name' ] + ' ' + conductorReserva[ 'last_name' ];
		}

		objectToSend = new Array();
		dataToSend = false;

		if (carReserva)
		{
			$(".car-data-form").each(function() {

				if (carReserva[this.name] !== this.value)
					objectToSend.push(Array(this.name, this.value));
			});
			if (objectToSend.length) {
				car = carReserva['idcar'];
				objectToSend.push(Array('idcar', carReserva['idcar']));
				dataToSend = {type: 'updateCar', car: objectToSend};
			}

		}
		else {
			$(".car-data-form").each(function() {

				objectToSend.push(Array(this.name, this.value));
			});
			dataToSend = {type: 'insert', car: objectToSend, enterprise: true};
		}
		if (dataToSend) {
			$.ajax({
				beforeSend: function() {
					$('#okR').replaceWith('<img id="loading" style="width: 25px; height: 25px; margin-left: 10px" src="images/loading.gif"></img>');
				},
				complete: function() {
					$('#loading').replaceWith('<div id="okR" class="button_okR " style=" cursor: pointer; margin-top: 3px;" onclick="reservationEnterprisesFormValidation()"><label style="margin-left: 10px;cursor: inherit;">Login</label></div>');
				},
				dataType: "json",
				async: false,
				type: "POST",
				url: "../phpFiles/index.php",
				data: dataToSend
			}).done(function(msg) {

				if (msg)
				{
					car = carReserva['idcar'];
					$.each(msg, function(key, val) {

						dataConfirm.reservation[ key ] = val;
					});
				}

			});
		}
		else {
			car = carReserva['idcar'];
			dataConfirm.reservation[ "mark" ] = carReserva[ "mark" ];
			dataConfirm.reservation[ "model" ] = carReserva[ "model" ];
			dataConfirm.reservation[ "plate" ] = carReserva[ "plate" ];
			dataConfirm.reservation[ "color" ] = carReserva[ "color" ];

		}

		$(".reservation-data-form").each(function() {

			reservation.push(Array(this.name, this.value));

			dataConfirm.reservation[ this.name ] = this.value;
		});
		reservation.push(Array('baggage', $("[name='baggage']:checked").val()));
		dataConfirm.reservation[ 'baggage' ] = $("[name='baggage']:checked").val();
		dataConfirm.reservation[ 'emailConfirm' ] = $("#userEmail").val();
		var charge = getCharge(dataConfirm.reservation[ 'day_date_car_in' ], dataConfirm.reservation[ 'day_date_car_out' ]);
		if (charge) {

			var serviceCharge = 0;
			if ((typeof $("input[name='washing']:checked").val() !== "undefined")) {

				reservation.push(Array("washing", $("input[name='washing']:checked").attr('id')));
				serviceCharge += parseInt($("input[name='washing']:checked").val());
				if ($("#service1").length)
					$("#service1").replaceWith('<tr id="service1"><td><label class="displayContentConfirm" style="width: 10px" >1</label></td><td style="width: 200px"><label class="displayContentConfirm" style="width: 300px;" >Lavage a la main: extérieur</label></td><td></td><td><label class="displayContentConfirm"><strong>CHF</strong></label><label class="displayContentConfirm" style="width: 40px; text-align: right; margin-right: 10px; float: right"><strong>' + $("input[name='washing']:checked").val() + ',00</strong></label></td></tr>');
				else
					$('<tr id="service1"><td><label class="displayContentConfirm" style="width: 10px" >1</label></td><td style="width: 200px"><label class="displayContentConfirm" style="width: 300px;" >Lavage a la main: extérieur</label></td><td></td><td><label class="displayContentConfirm"><strong>CHF</strong></label><label class="displayContentConfirm" style="width: 40px; text-align: right; margin-right: 10px; float: right"><strong>' + $("input[name='washing']:checked").val() + ',00</strong></label></td></tr>').insertBefore("#serviceMark");
			}
			else {
				if ($("#service1").length)
					$("#service1").replaceWith('');
			}
			if ((typeof $("input[name='aspirate']:checked").val() !== "undefined")) {

				reservation.push(Array("aspirate", $("input[name='aspirate']:checked").attr('id')));
				serviceCharge += parseInt($("input[name='aspirate']:checked").val());
				if ($("#service2").length)
					$("#service2").replaceWith('<tr id="service2"><td><label class="displayContentConfirm" style="width: 10px" >1</label></td><td style="width: 200px"><label class="displayContentConfirm" style="width: 300px;" >Aspirateur</label></td><td></td><td><label class="displayContentConfirm"><strong>CHF</strong></label><label class="displayContentConfirm" style="width: 40px; text-align: right; margin-right: 10px; float: right"><strong>' + $("input[name='aspirate']:checked").val() + ',00</strong></label></td></tr>');
				else
					$('<tr id="service2"><td><label class="displayContentConfirm" style="width: 10px" >1</label></td><td style="width: 200px"><label class="displayContentConfirm" style="width: 300px;" >Aspirateur</label></td><td></td><td><label class="displayContentConfirm"><strong>CHF</strong></label><label class="displayContentConfirm" style="width: 40px; text-align: right; margin-right: 10px; float: right"><strong>' + $("input[name='aspirate']:checked").val() + ',00</strong></label></td></tr>').insertBefore("#serviceMark");
			}
			else {
				if ($("#service2").length)
					$("#service2").replaceWith('');
			}

			var total = parseInt(charge.charge + serviceCharge), tarifV = parseFloat(charge.charge - 25);
			reservation.push(Array('charge', total, 'days_car_in', charge.days));
			reservation.push(Array('days_car_in', charge.days));

			dataConfirm.reservation[ 'charge' ] = '<label class="displayContentConfirm">CHF</label><label class="displayContentConfirm" style="width: 40px; text-align: right; margin-right: 10px; float: right">' + tarifV + ',00</label>';
			dataConfirm.reservation[ 'days' ] = charge.days;
			dataConfirm.reservation[ 'total' ] = '<label class="displayContentConfirm"><strong>CHF</strong></label><label class="displayContentConfirm" style="width: 40px; text-align: right; margin-right: 10px; float: right"><strong>' + total + ',00</strong></label>';
			$(".confirm-data-reservation").each(function() {

				if (this.id === "day_date_car_in") {

					$(this).html("<strong>" + dataConfirm.reservation[ this.id ] + " " + dataConfirm.reservation[ 'hour_car_in' ] + "</strong>");
				}
				else if (this.id === "date_fly") {

					$(this).html("<strong>" + dataConfirm.reservation[ this.id ] + " " + dataConfirm.reservation[ 'hour_fly' ] + "</strong>");
				}
				else if (this.id === "day_date_car_out") {

					$(this).html("<strong>" + dataConfirm.reservation[ this.id ] + " " + dataConfirm.reservation[ 'hour_car_out' ] + "</strong>");
				}
				else if (this.id === "date_fly_back") {

					$(this).html("<strong>" + dataConfirm.reservation[ this.id ] + " " + dataConfirm.reservation[ 'hour_fly_back' ] + "</strong>");
				}
				else if (this.id === "phone_movil2") {

					$(this).html("Mobile: <strong>" + dataConfirm.reservation[ "phone_movil" ] + "</strong>");
				}
//				else if (this.id === "emailConfirm") {
//
//					$(this).html("Email: <strong>" + dataConfirm.reservation[ this.id ] + "</strong>");
//				}
				else if (this.id === "days") {

					$(this).html(dataConfirm.reservation[ this.id ]);
				}
				else if (this.id === "sex") {

					$(this).html("<strong>" + dataConfirm.reservation[ this.id ] + ".</strong>");
				}
				else if (this.id === "address") {

					$(this).html("Adresses (Rue et No): <strong>" + dataConfirm.reservation[ this.id ] + "</strong>");
				}
				else if (this.id === "postal_code") {

					$(this).html("C.P/localité: <strong>" + dataConfirm.reservation[ this.id ] + "</strong>");
				}
				else {

					$(this).html("<strong>" + dataConfirm.reservation[ this.id ] + "</strong>");
				}


			});
			reservationtweenConfirm.play();
			TweenLite.to(window, 1, {scrollTo: {y: 0}});
		}
		else {

			alert("Les dates sont incorrects.");
		}

	}
}

function userEnterpriseFormValidation() {

	if (userform.form()) {

		var objectToSend = new Array(), fail = false;
		if (userenterprise) {
			$(".user-data-form").each(function() {

				if (userenterprise[this.name] !== this.value)
				{
					objectToSend.push(Array(this.name, this.value));
				}
			});
			if (objectToSend.length) {

				objectToSend.push(Array('userid', userenterprise['userid']));
				dataToSend = {type: 'updateUser', user: objectToSend};

			}
		}
		else {
			$(".user-data-form").each(function() {

				objectToSend.push(Array(this.name, this.value));
			});
			dataToSend = {type: 'insert', user: objectToSend};
		}
		if (typeof (dataToSend) !== 'undefined') {
			$.ajax({
				beforeSend: function() {
					$('#userE').replaceWith('<img id="loading" style="width: 25px; height: 25px; margin-left: 10px" src="images/loading.gif"></img>');
				},
				complete: function() {
					$('#loading').replaceWith('<div id="userE" class="button_okR " style=" cursor: pointer; margin-top: 3px;" onclick="userEnterpriseFormValidation()"><label style="margin-left: 10px;cursor: inherit;">Login</label></div>');
				},
				dataType: "json",
				type: "POST",
				url: "../phpFiles/index.php",
				data: dataToSend
			}).done(function(msg) {

				if (msg)
				{

					if (typeof (msg['namec']) !== 'undefined') {
						$('#conductor_name').append('<option value="' + msg['conductorid'] + '" >' + msg['sexc'] + ' ' + msg['namec'] + ' ' + msg['last_namec'] + '</option>');

						if (conductorsData) {
							conductorsData.push({
								"conductorid": msg['conductorid'],
								"sex": msg['sexc'],
								"name": msg['namec'],
								"last_name": msg['last_namec'],
								"postal_code": msg['postal_codec'],
								"address": msg['addressc'],
								"phone_movil": msg['phone_movilc']
							});
						}
						else {

							conductorsData = [{
									"conductorid": msg['conductorid'],
									"sex": msg['sexc'],
									"name": msg['namec'],
									"last_name": msg['last_namec'],
									"postal_code": msg['postal_codec'],
									"address": msg['addressc'],
									"phone_movil": msg['phone_movilc']
								}];
						}
					}
					userReserva = msg;
					var fullName = "", name = "", sex = "", conductorid = "";
					$.each(msg, function(key, val) {

						if (key === "name") {

							name = val;
						}
						else if (key === 'last_name') {

							fullName += name + " " + val;
						}
						else if (key === 'sex') {

							sex = val;
						}
						else if (key === 'email') {

							$("#userEmail").val(val);
						}
						else if (key === 'conductorid') {

							conductorid = val;
						}
						else if (key === 'fail') {

							fail = true;
						}

					});

				}

			});
		}
		else {
			userReserva = userenterprise;

		}

		if (!fail) {
			$("#div-loguin-form").replaceWith('<label style="float: right; text-align: right; margin-right: 30px;color: #005599; font-size: 1.2em; z-index: 999">Bievenue ' + sex + ' ' + name + '<a href="" onmousedown="logout()"> déconnexion</a></label>');
			//$("<option value='" + conductorid + "' selected>" + sex + ' ' + name + "</option>").appendTo("#conductor_name");
			usertweencarenterprise.play();

		}
		else {
			alert("Oups .. un problème, s'il vous plaît réessayer plus tard. Merci.");
		}

	}

}

function sendReservationE() {

	$.ajax({
		beforeSend: function() {
			$('#sendR').replaceWith('<img id="loading" style="width: 25px; height: 25px; margin-left: 10px" src="images/loading.gif"></img>');
		},
		complete: function() {
			$('#loading').replaceWith('<div id="sendR" class="button_okR " style=" cursor: pointer; margin-top: 3px;" onclick="sendReservationE()"><label style="margin-left: 10px;cursor: inherit;">OK</label></div>');
		},
		type: "POST",
		url: "../phpFiles/index.php",
		data: {type: 'insert', reservation: reservation, car: car, conductor: conductor, userid: userReserva['userid']}
	}).done(function(msg) {
		if (msg)
		{
			reservationtweenConfirmFinal.play();
			window.scrollTo(0, 0);
		}

	});
}